from Products.Archetypes.public import listTypes
from Products.Archetypes.Extensions.utils import installTypes, install_subskin
from Products.ArchSurvey.config import *
from Products.ArchSurvey.questions import QuestionRegistry
from Products.CMFDynamicViewFTI.migrate import migrateFTIs
from Products.CMFCore.utils import getToolByName


from StringIO import StringIO

def install_workflow(self, out):
   wtool = self.portal_workflow
   
   wtool.manage_addWorkflow(id=survey_workflow_id, workflow_type=survey_workflow)
   wtool.setChainForPortalTypes(('Survey',), survey_workflow_id)
   
   wtool.manage_addWorkflow(id=survey_question_workflow_id,
	                        workflow_type=survey_question_workflow)
   question_types = tuple(QuestionRegistry.listTypes())
   wtool.setChainForPortalTypes(question_types, survey_question_workflow_id)

def updateSelectViewTemplate( self, out ):
   """Update the select view template destination for surveys"""
   tool = getToolByName( self, 'portal_form_controller')
   tool.addFormAction('selectViewTemplate', 'success', 'Survey', '', 'redirect_to_action', 'results')
      
def install(self):
    out = StringIO()

    installTypes(self, out,
                 listTypes(PROJECTNAME),
                 PROJECTNAME)

    install_subskin(self, out, GLOBALS)
    install_workflow(self, out)
	
    print >> out, "Successfully installed %s." % PROJECTNAME

    migrated = migrateFTIs(self, product=PROJECTNAME)
    print >>out, "Switched to DynamicViewFTI: %s" % ', '.join(migrated)

    return out.getvalue()
